%{
resave variables from nc file to yearly mat file
%}

%%% define the file name of all models 
model = { 'albm', 'lake', 'simstrat-uog' };
gcm = { 'gfdl-esm2m', 'hadgem2-es', 'ipsl-cm5a-lr', 'miroc5' };
rcp = { 'historical', 'picontrol', 'rcp26', 'rcp60', 'rcp85' };
soc = { '2005soc', 'nosoc', 'nosoc' };
var = { 'watertemp', 'surftemp', 'surftemp'};	% define the var.

%%% albm
flag_model = 1;
% nc file time
time_list_file_sta{flag_model} = { datetime([1861:10:2001], 1, 1), datetime([2006,2011:10:2091], 1, 1), ...
                datetime([2006,2011:10:2091], 1, 1), datetime([2006,2011:10:2091], 1, 1), datetime([2006,2011:10:2091], 1, 1) };     % for 5 rcp
time_list_file_end{flag_model} = { datetime([1870:10:2000,2005], 12, 31), datetime([2010:10:2090,2099], 12, 31), ...
                datetime([2010:10:2090,2099], 12, 31), datetime([2010:10:2090,2099], 12, 31), datetime([2010:10:2090,2099], 12, 31) };     % for 5 rcp
% annual time
time_serises_sta{flag_model} = { datetime([1861:2005], 1, 1), datetime([2006:2099], 1, 1), ...
                datetime([2006:2099], 1, 1), datetime([2006:2099], 1, 1), datetime([2006:2099], 1, 1) };   % for 5 rcp
time_serises_end{flag_model} = cellfun( @(x) x + calmonths(12) - days(1), time_serises_sta{flag_model}, 'UniformOutput', false );

% lake
flag_model = 2;
% nc file time
time_list_file_sta{flag_model} = { datetime([1861:10:2001], 1, 1), datetime([1661:10:1851], 1, 1), ...
                datetime([2006,2011:10:2091], 1, 1), datetime([2006,2011:10:2091], 1, 1), datetime([2006,2011:10:2091], 1, 1) };     % for 5 rcp
time_list_file_end{flag_model} = { datetime([1870:10:2000,2005], 12, 31), datetime([1670:10:1860], 12, 31), ...
                datetime([2010:10:2090,2099], 12, 31), datetime([2010:10:2090,2099], 12, 31), datetime([2010:10:2090,2099], 12, 31) };     % for 5 rcp
% annual time
time_serises_sta{flag_model} = { datetime([1861:2005], 1, 1), datetime([1661:1860], 1, 1), ...
                datetime([2006:2099], 1, 1), datetime([2006:2099], 1, 1), datetime([2006:2099], 1, 1) };   % for 5 rcp
time_serises_end{flag_model} = cellfun( @(x) x + calmonths(12) - days(1), time_serises_sta{flag_model}, 'UniformOutput', false );

% simstrat-uog
flag_model = 3;
% nc file time
time_list_file_sta{flag_model} = { datetime([1861:10:2001], 1, 1), datetime([1661:10:1851], 1, 1), ...
                datetime([2006,2011:10:2091], 1, 1), datetime([2006,2011:10:2091], 1, 1), datetime([2006,2011:10:2091], 1, 1) };     % for 5 rcp
time_list_file_end{flag_model} = { datetime([1870:10:2000,2005], 12, 31), datetime([1670:10:1860], 12, 31), ...
                datetime([2010:10:2090,2099], 12, 31), datetime([2010:10:2090,2099], 12, 31), datetime([2010:10:2090,2099], 12, 31) };     % for 5 rcp
% annual time
time_serises_sta{flag_model} = { datetime([1861:2005], 1, 1), datetime([1661:1860], 1, 1), ...
                datetime([2006:2099], 1, 1), datetime([2006:2099], 1, 1), datetime([2006:2099], 1, 1) };   % for 5 rcp
time_serises_end{flag_model} = cellfun( @(x) x + calmonths(12) - days(1), time_serises_sta{flag_model}, 'UniformOutput', false );


%% save output data (nc) to yearly data (mat) 

tic

for flag_model = [1:3]      %   #1 = albm       #2 = lake      #3 = simstrat-uog
    
    for flag_gcm = 1:4      %   #1 = gfdl-esm2m      #2 = hadgem2-es      #3 = ipsl-cm5a-lr      #4 = miroc5

        for flag_rcp = 1:5     %    #1 = historical       #2 = picontrol        #3 = rcp26      #4 = rcp60      #5 = rcp85

            for flag_time = 1:size( time_serises_sta{flag_model}{ flag_rcp }, 2 )

                %%% the var to save
                surftemp = [];

                %%% show the timestep
                disp( [ char( time_serises_sta{flag_model}{ flag_rcp }(flag_time) ), ' to ', char( time_serises_end{flag_model}{ flag_rcp }(flag_time) ) ] )

                %%% select the original nc file which inclouded the target year
                flag_time_file = find( time_serises_sta{flag_model}{ flag_rcp }(flag_time) <= time_list_file_end{flag_model}{flag_rcp}, 1 );  
                sta_time_file = time_list_file_sta{flag_model}{flag_rcp}(flag_time_file);   % start time of original nc file
                end_time_file = time_list_file_end{flag_model}{flag_rcp}(flag_time_file);   % emd time of original nc file

                %%% file name for output(lake model data) 
                loc_input = [ '/home/yuanlin/NAS/湖气温差研究/ISIMIP/output/', model{flag_model}, '/', gcm{flag_gcm}, '/' ];
                file_input = [ model{flag_model},   '_', gcm{flag_gcm},   '_ewembi_',   rcp{flag_rcp}, ...
                             '_',   soc{flag_model},   '_co2_', var{flag_model}, '_global_daily_', ... 
                             char( sta_time_file, 'yyyy' ), '_', ...
                             char( end_time_file, 'yyyy' ), '.nc4' ];
                         
                %%% find the location of the target year in the original nc file
                sta_loc = days( time_serises_sta{flag_model}{ flag_rcp }(flag_time) - sta_time_file + days(1) );
                end_loc = days( time_serises_end{flag_model}{ flag_rcp }(flag_time) - sta_time_file + days(1) );
                step_loc =  end_loc - sta_loc +1; 

                %%% Source 
                source = [ file_input, ', Zvlue_', num2str(sta_loc), '-', num2str(end_loc) ];
                disp( source )

                %%% read nc file
                if flag_model <= 1      % ALBM model have multilayer 
                    surftemp = ncread( [loc_input, file_input], var{flag_model}, [1 1 1 sta_loc], [Inf, Inf, 1, step_loc] );
                    surftemp = rot90( flipud( squeeze( surftemp ) ), 3 );
                else
                    surftemp = ncread( [loc_input, file_input], var{flag_model}, [1 1 sta_loc], [Inf, Inf, step_loc] );
                    surftemp = rot90( flipud( surftemp ), 3 );
                end

                %%% save data to yearly and mat type     
                file_save = [ model{flag_model},   '_', gcm{flag_gcm}, '_', rcp{flag_rcp}, '_', 'surftemp_daily_', ...
                            char( time_serises_sta{flag_model}{ flag_rcp }(flag_time),'yyyy' ), '.mat'];
                save( [loc_input, rcp{flag_rcp}, '/', file_save], 'surftemp', 'source' )

            end

        end

    end

end

toc















